/*
 * Copyright (c) 2009 Nathaniel Houghton <nathan@brainwerk.org>
 *                    Robert Kirchgessner <rkirchge@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for
 * any purpose with or without fee is hereby granted, provided that
 * the above copyright notice and this permission notice appear in all
 * copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 */

#ifndef UI_H
#define UI_H

#include <stdint.h>

#include "mode.h"

#define IGNITION_MSK 0x10

#define BTN_1 0x10
#define BTN_2 0x08
#define BTN_3 0x04
#define BTN_IGNITION 0x02
#define BTN_LCDPRESENT 0x01

#define MENU_ENTRY_STATIC 0x0
#define MENU_ENTRY_DYNAMIC 0x1

#define LCD_LINE_LENGTH 20

#define BUTTON_LEFT BTN_3
#define BUTTON_RIGHT BTN_1
#define BUTTON_ENTER BTN_2

#define MENU_ENTRY_COUNT		13   /*Remember to update me*/
#define MENU_ID_ROOT 			0x00
#define MENU_ID_CHECK_DTC 		0x01
#define MENU_ID_DTCMIL			0x02
#define MENU_ID_VIEWDTC			0x03
#define MENU_ID_CLEARDTC		0x04
#define MENU_ID_BACK			0x05
#define MENU_ID_DTC				0x06
#define MENU_ID_DTCRIGHT		0x07
#define MENU_ID_DTCLEFT			0x08
#define MENU_ID_FLASH_STATUS 	0x09
#define MENU_ID_NVM_ERASE 		0x0A
#define MENU_ID_ERASED 			0x0B
#define MENU_ID_CLEAREDDTC		0x0C

extern struct lcd lcd;

struct lcd {
	uint8_t initd;
	char line0[LCD_LINE_LENGTH+1];
	char line1[LCD_LINE_LENGTH+1];
};

struct menu_entry {
	uint8_t type; /*Type of menu entry*/
	uint8_t id;
	uint8_t left; /*Integer identifier to the next menu to display when the left button is pressed*/
	uint8_t right; /*Integer identifier to the next menu to display when the right button is pressed*/
	uint8_t enter; /*Integer identifier to next menu to display*/
	void (*fp)(void); /*Pointer to the function  to be run if enter pressed*/
};

struct static_menu_entry {
	uint8_t type; /*Type of menu entry*/
	uint8_t id;
	uint8_t left; /*Integer identifier to the next menu to display when the left button is pressed*/
	uint8_t right; /*Integer identifier to the next menu to display when the right button is pressed*/
	uint8_t enter; /*Integer identifier to next menu to display*/
	
	void (*fp)(void); /*Pointer to the function  to be run if enter pressed*/
	
	char disp[]; /*Pointer to string to display*/
};

struct dynamic_menu_entry {
	uint8_t type; /*Type of menu entry*/
	uint8_t id;
	uint8_t left; /*Integer identifier to the next menu to display when the left button is pressed*/
	uint8_t right; /*Integer identifier to the next menu to display when the right button is pressed*/
	uint8_t enter; /*Integer identifier to next menu to display*/
	
	void (*fp)(void); /*Pointer to the function  to be run if enter pressed*/
	
	void (*disp)(void); /*Pointer to the function that will generate the message*/
};

struct menu {
	uint8_t current; /*current menu being displayed*/

	struct menu_entry *menu_entries[]; /*All the menu entries in the menu*/	
};

#define MENU_RES_NAME(ID) _RES_##ID
#define MENU_ENTRY(ID) (struct menu_entry*)&_RES_##ID
#define MENU_RES_STATIC(ID, STR, LEFT, RIGHT, ENTER, FP) const struct static_menu_entry _RES_##ID = { MENU_ENTRY_STATIC, ID, LEFT, RIGHT, ENTER, FP, STR }
#define MENU_RES_DYNAMIC(ID, DISP, LEFT, RIGHT, ENTER, FP) const struct dynamic_menu_entry _RES_##ID = { MENU_ENTRY_DYNAMIC, ID, LEFT, RIGHT, ENTER, FP, DISP }

void lcd_update(struct lcd *, uint8_t, char *);
void init_ui(void);
void menu(struct mode *, uint8_t);
uint8_t btn_down(uint8_t);
void menu_repaint(void);

void menu_dynamic_root(void);
void menu_dynamic_dtcmil(void);
void menu_dtc_update(void);
void menu_dtc_current(void);
void menu_dynamic_flash(void);
void menu_set_current( uint8_t );

#endif
